﻿using System;
using System.Data;
using System.Windows.Forms;
using UniMana.Data;
using UniMana.Utils.Data;
using UniMana.Utils.Controls;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using UniMana.Utils.LoginInfo;
using System.Linq;
using UniMana.Utils.Helper;


namespace UniMana.Win.Personnel
{
    public partial class FrmPersonelList : DevExpress.XtraEditors.XtraForm
    {
        private DBMain m_data = new DBMain();
        private DBMain m_Catalogue = new DBMain();
        private bds m_bds = new bds();
        private DataTable m_dt = new DataTable();

        private string m_Rule = UniMana.Utils.Configs.RuleConfig.LECTUREINFO;
        public FrmPersonelList()
        {

            if (!SysUtils.CheckAccessByRule(m_Rule))
            {
                return;
            }
            InitializeComponent();
            SysUtils.SetRuleToXucBar(xucBar1, this.m_Rule);

            LoadData();
            InitControlEvent();
        }
        #region [Ultis]
        public void LoadData()
        {
            //Đơn vị
            if (GblLoginInfo.iListDepartment.Count == 1)
            {
                lueCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments.Where(p => p.Catalogue_DepartmentKey == GblLoginInfo.iDepartmentKey));
                try
                {
                    lueCatalogue_Department.EditValue = GblLoginInfo.iDepartmentKey;
                    lueCatalogue_Department.Enabled = false;
                }
                catch { }
            }
            else
            {
                var q = from dpt in m_data.m_DCReadOnly.Catalogue_Departments
                        where GblLoginInfo.iListDepartment.Contains(dpt.Catalogue_DepartmentKey)
                        select dpt;
                lueCatalogue_Department.GetData(q);
            }

            lueCatalogue_Department.Properties.NullText = "---Chọn tất cả---";
        }
        #endregion
        #region[Events]
        public void InitControlEvent()
        {
            //////xucBar1.btnAddNew_Click += new UniMana.Controls.xucBar.ButtonClickHandler(this.btnAddNew_Click);
            //////xucBar1.btnRefresh_Click += new UniMana.Controls.xucBar.ButtonClickHandler(this.btnRefresh_Click);
            //////xucBar1.btnExport_Click += new UniMana.Controls.xucBar.ButtonClickHandler(this.btnExportExcel_Click);
            //////xucBar1.btnPrint_Click += new UniMana.Controls.xucBar.ButtonClickHandler(this.btnPrint_Click);
            //////xucBar1.btnDelete_Click += new UniMana.Controls.xucBar.ButtonClickHandler(this.btnDelete_Click);
            //xucBar1.btnSave_Click += new UniMana.Controls.xucBar.ButtonClickHandler(this.btnSave_ItemClick);
            //xucBar1.btnCancel_Click += new UniMana.Controls.xucBar.ButtonClickHandler(btnCancel_Click);
            //xucBar1.btnImport_Click += new UniMana.Controls.xucBar.ButtonClickHandler(btnImportExcel_ItemClick);
        }
        #endregion
        //Buttons
        private void btnAddNew_Click(object sender, ItemClickEventArgs e)
        {
            if (!SysUtils.CheckAddByRule(this.m_Rule))
            {
                xucBar1.dlg.Close();
                return;
            }
            xucBar1.dlg.Close();
            FrmPersonelList_SubInfo f = new FrmPersonelList_SubInfo();
            Guid newKey = Guid.NewGuid();
            Personnel_PersonnelInfo info = new Personnel_PersonnelInfo(newKey);
            f.m_personelInfo = info;
            f.ShowDialog();
        }
        private void btnDelete_Click(object sender, ItemClickEventArgs e)
        {
            if (!SysUtils.CheckDeleteByRule(this.m_Rule))
            {
                xucBar1.dlg.Close();
                return;
            }

            ////////if(m_data.m_DCReadOnly.Personnel_Personnels.CheckDelete(m_Catalogue.m_DCReadOnly.Personnel_Contracts, m_Catalogue.m_DCReadOnly.Personnel_Lecturers,
            ////////    m_Catalogue.m_DCReadOnly.Personnel_Personnel_Profiles,Guid.Parse( grvPersonel.GetFocusedRowCellValue(grvPersonel.Columns["Personnel_PersonnelKey"]).ToString())) == false)

            ////////{
                DialogResult diag = XtraMessageBox.Show("Bạn có muốn xóa không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                switch (diag)
                {
                    case DialogResult.Yes:
                        {
                            Guid personelKey =Guid.Parse(grvPersonel.GetFocusedRowCellValue(grvPersonel.Columns["Personnel_PersonnelKey"]).ToString());
                            Personnel_PersonnelInfo info = new Personnel_PersonnelInfo(personelKey);
                            info.Delete();
                            info.Save();
                            XtraMessageBox.Show("Xóa thành công!", "Thông báo");
                            break;
                        }
                    case DialogResult.No:
                        {
                            break;
                        }
                }
            ////}
            ////else
            ////{
                XtraMessageBox.Show("Không thể xóa! \nDữ liệu đang được sử dụng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            ////}
        }
        private void btnPrint_Click(object sender, ItemClickEventArgs e)
        {
            if (!SysUtils.CheckPrintByRule(this.m_Rule))
            {
                xucBar1.dlg.Close();
                return;
            }


            if (grvPersonel.RowCount> 0)
            {
                bds b = new bds();
                b.DataSource = grvPersonel.DataSource;
                FrmPersonelList_RptList rpt = new FrmPersonelList_RptList(b);
                rpt.ShowPreview();
            }
            else
            {
                XtraMessageBox.Show("Không thể in khi số dòng dữ liệu bằng 0", "Lỗi khi in", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btnRefresh_Click(object sender, ItemClickEventArgs e)
        {
            lueCatalogue_Department.EditValue = null;
            gctPersonel.DataSource = null;
        }
        private void btnExportExcel_Click(object sender, ItemClickEventArgs e)
        {
            if (!SysUtils.CheckExportByRule(this.m_Rule))
            {
                xucBar1.dlg.Close();
                return;
            }

            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.FileName = "Danh sách nhân viên_" + DateTime.Now.Date.ToShortDateString().Replace('/', '_');
            saveFile.Title = "Xuất ra tệp tin";
            saveFile.DefaultExt = "xls";
            saveFile.Filter = "Excel 2003 (*.xls)|";
            if (saveFile.ShowDialog() == DialogResult.OK)
            {
                grvPersonel.ExportToXls(saveFile.FileName);
            }
        }
        private void btnView_Click(object sender, EventArgs e)
        {
            getInfo();
        }
        //GridView
        private void grvPersonel_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            int rowIndex = e.RowHandle;
            if (rowIndex >= 0)
            {
                rowIndex++;
                e.Info.DisplayText = rowIndex.ToString();
            }
        }
        private void grvPersonel_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                Guid personelKey =Guid.Parse( grvPersonel.GetFocusedRowCellValue(grvPersonel.Columns["Personnel_PersonnelKey"]).ToString());
                FrmPersonelList_SubInfo f = new FrmPersonelList_SubInfo();
                Personnel_PersonnelInfo info = new Personnel_PersonnelInfo(personelKey);
                f.m_personelInfo = info;
                f.m_Flag = false;
                f.ShowDialog();
            }
            catch { }
        }
        //Methods
        private void getInfo()
        {
            grvPersonel.BeginUpdate();
            int deptKey = lueCatalogue_Department.GetIntValue();
            int prsType = -1;
            if (ckeCH.Checked) prsType = 1; //Lọc giảng viên cơ hữu
            if (ckeTG.Checked) prsType = 2;//Lọc giảng viên thỉnh giảng

            int teach =-1;
            if(ckeTeach.Checked) teach = 1;
            if(ckeNotTeach.Checked) teach =0;

            m_dt = m_data.m_DCReadOnly.Personnel_Personnels.GetPersonelListByCodition(m_data.m_DCReadOnly.Catalogue_Departments,
                                                                        m_data.m_DCReadOnly.Catalogue_Positions,
                                                                        deptKey, prsType, teach).ToDataTable();
            gctPersonel.DataSource = m_dt;
            grvPersonel.EndUpdate();
        }

        #region [Các checkbox lọc dữ liệu thay đổi giá trị]
        private void ckeNotTeach_CheckedChanged(object sender, EventArgs e)
        {
            if (ckeNotTeach.Checked)
            {
                ckeTeach.Checked = false;
                ckeTG.Checked = false;
                ckeCH.Checked = false;
            }
        }

        private void ckeTeach_CheckedChanged(object sender, EventArgs e)
        {
            if (ckeTeach.Checked)
                ckeNotTeach.Checked = false;
        }

        private void ckeTG_CheckedChanged(object sender, EventArgs e)
        {
            if (ckeTG.Checked)
            {
                ckeCH.Checked = false;
                ckeTeach.Checked = true;
            }
        }

        private void ckeCH_CheckedChanged(object sender, EventArgs e)
        {
            if (ckeCH.Checked)
            {
                ckeTG.Checked = false;
                ckeTeach.Checked = true;
            }
        }
        #endregion

    }
}
